package com.itheima.leyou.service;

import com.itheima.leyou.dao.StorageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class StorageService {

    @Resource
    private StorageDao storageDao;

    public Map<String, Object> getStorageQuanty(String sku_id){
        Map<String, Object> map = new HashMap<>();

        if (sku_id==null||sku_id.equals("")){
            map.put("result", false);
            map.put("msg", "传入参数有误，请刷新数据！");
            return map;
        }

        ArrayList<Map<String, Object>> list = new ArrayList<>();
        list = storageDao.getStorageQuanty(sku_id);

        map.put("storage", list);
        map.put("result", true);
        map.put("msg", "");
        return map;
    }

    @Transactional
    public Map<String, Object> insertStorage(Integer warehouse_id, String sku_id, double inquanty, double outquanty){
        Map<String, Object> map = new HashMap<>();

        if (warehouse_id==0){
            map.put("result", false);
            map.put("msg", "库房不能为空！");
            return map;
        }

        if (sku_id.equals("")){
            map.put("result", false);
            map.put("msg", "商品的sku不能为空！");
            return map;
        }

        if (inquanty<0||outquanty<0){
            map.put("result", false);
            map.put("msg", "入库数量和出库数量不能小于0！");
            return map;
        }

        return storageDao.insertStorage(warehouse_id, sku_id, inquanty, outquanty);
    }

}
